package demo.practice.bfs;

import demo.practice.TreeNode;

import java.util.*;

public class P199 {

    public static void main(String[] args) {
        P199 p199= new P199();
        TreeNode root= new TreeNode(1);
        root.right= new TreeNode(3);
        root.right.right= new TreeNode(4);

        root.left=new TreeNode(2);
        root.left.right= new TreeNode(5);
        p199.rightSideView(root);
    }

    public List<Integer> rightSideView(TreeNode root) {

        List<Integer> list= new ArrayList<>();
        if(root== null)
            return list;
        Queue<TreeNode> queue= new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()){
            int len= queue.size();
            list.add(queue.peek().val);
            while (len>0){
                TreeNode top = queue.poll();
                if(top.right!=null)
                    queue.add(top.right);
                if(top.left!=null)
                    queue.add(top.left);
                len--;
            }
        }
        return list;
    }
}
